import request from '@/utils/request'

// 楼层信息
export interface FloorData {
  id: number
  name: string
  level: number
  description?: string
  status: 'ACTIVE' | 'INACTIVE'
  venueId: number
  createdAt: string
  updatedAt: string
}

// 演出信息
export interface PerformanceData {
  id: number
  title: string
  startTime: string
  endTime: string
  status: 'SCHEDULED' | 'ONGOING' | 'COMPLETED' | 'CANCELLED'
  venueId: number
  artist?: {
    id: number
    name: string
    avatar?: string
  }
  createdAt: string
  updatedAt: string
}

// 关联计数
export interface VenueCounts {
  floors: number
  performances: number
}

export interface VenueData {
  id?: number
  name: string
  description?: string
  address: string
  city: string
  country: string
  capacity?: number
  sortOrder?: number
  contactPhone?: string
  contactEmail?: string
  website?: string
  latitude?: number
  longitude?: number
  status: 'ACTIVE' | 'INACTIVE' | 'MAINTENANCE'
  createdAt?: string
  updatedAt?: string
  floors?: FloorData[]
  performances?: PerformanceData[]
  _count?: VenueCounts
}

export interface VenueQuery {
  page?: number
  pageSize?: number
  keyword?: string
  status?: string
  city?: string
  country?: string
  sortBy?: 'sortOrder' | 'name' | 'createdAt' | 'city'
  sortOrder?: 'asc' | 'desc'
}

export interface VenueStats {
  total: number
  active: number
  inactive: number
  maintenance: number
  cityStats: Array<{ city: string; count: number }>
  countryStats: Array<{ country: string; count: number }>
}

// 后端返回的通用响应结构
export interface ApiResponse<T> {
  success: boolean
  code: number
  message: string
  data: T
  timestamp: string
}

// 场馆列表数据
export interface VenueListData {
  data: VenueData[]
  pagination: {
    total: number
    page: number
    pageSize: number
    totalPages: number
  }
}

// 场馆列表响应
export type VenueListResponse = ApiResponse<VenueListData>

// 获取场馆列表
export function getVenues(params: VenueQuery): Promise<VenueListResponse> {
  return request({
    url: '/admin/venues',
    method: 'get',
    params
  })
}

// 获取场馆详情
export function getVenueById(id: number): Promise<ApiResponse<VenueData>> {
  return request({
    url: `/admin/venues/${id}`,
    method: 'get'
  })
}

// 创建场馆
export function createVenue(data: Omit<VenueData, 'id' | 'createdAt' | 'updatedAt'>): Promise<ApiResponse<VenueData>> {
  return request({
    url: '/admin/venues',
    method: 'post',
    data
  })
}

// 更新场馆
export function updateVenue(id: number, data: Partial<VenueData>): Promise<ApiResponse<VenueData>> {
  return request({
    url: `/admin/venues/${id}`,
    method: 'put',
    data
  })
}

// 删除场馆
export function deleteVenue(id: number): Promise<ApiResponse<void>> {
  return request({
    url: `/admin/venues/${id}`,
    method: 'delete'
  })
}

// 批量更新场馆状态
export function batchUpdateVenueStatus(ids: number[], status: string): Promise<ApiResponse<{ updatedCount: number }>> {
  return request({
    url: '/admin/venues/batch-status',
    method: 'put',
    data: { ids, status }
  })
}

// 获取场馆统计信息
export function getVenueStats(): Promise<ApiResponse<VenueStats>> {
  return request({
    url: '/admin/venues/stats',
    method: 'get'
  })
}
